      SUBROUTINE OUTPUTMSH(X,Y,XM,YM,FI,DFI,potno,dpotno,CX,CY,SOL,DSOL,
     &             INC,NNINC,NLD, INCD,NelD,NPI,C0,N0,NINCL,N_Eq,NN,
     &             L,LEC,file_bas,nu_msc,nu_msd,nu_trip,nu_trif)

      IMPLICIT DOUBLE PRECISION (A-H,O-Z)
      INCLUDE 'param_dim.inc'

      CHARACTER*9 file_bas
      DIMENSION XM(NX),YM(NX),FI(NX),DFI(NX)
      DIMENSION SOL(NPIX),DSOL(NPIX,2)
      DIMENSION CX(NPIX),CY(NPIX),INCD(NELDX,3)

      DIMENSION NNINC(ninc_max),CINC(ninc_max),NLD(ninc_max+1,2)

      dimension X(NX),Y(NX),INC(NX,2)

      dimension potno(nx),dpotno(nx,2)
      dimension pot(3),dpot(3,2),coord(3,2)


      ZERO= 0.0

!       WRITE(IMP,100)
!   100 FORMAT(' ',80('*')//1X,'RESULTADOS'//,
!      *       5X,'NOS DO CONTORNO'//11X,'X',
!      *       13X,'Y',9X,'POTENCIAL (u)',7X,'D(u)/Dn'/)
! 
!       DO 10 I=1,NN
!          WRITE(*,200)I,XM(I),YM(I),FI(I),DFI(I)
!   200    FORMAT(i5,2(5X,E10.4),2(5X,E12.5))
!    10 CONTINUE




      if(NPI.gt.0) then


C     *************************************     
C     *************************************     

C     Implementacao para inclusoes em desenvolvimento

C     *************************************     
C     *************************************     



         do 50 idom= 1,nincl+1

C     Determina os nos inicial e final dos contornos de cada dom�nio
            IF(IDOM.EQ.1) THEN
               NPTI_I= nld(idom,1) - nn
               NPTI_F= nld(idom,2) - nn
               
               NEL_I= 1
               NEL_F= NN
            ELSE
               NPTI_I= nld(idom,1) - nn
               NPTI_F= nld(idom,2) - nn

               IF(IDOM.EQ.2) NEL_F = N0
               NEL_I= NEL_F + 1
               NEL_F= NEL_F + NNINC(IDOM-1)

            ENDIF


!          WRITE(*,300) idom
!  300     FORMAT(//,2X,'PONTOS INTERNOS  DOM�NIO:',I4,/,
!      &          3X,'NO',10x,'X',14X,'Y',9X,'POTENCIAL',
!      &          8X,'Du/Dx',10X,'Du/Dy',/)
! 
!          DO 20 K=NPTI_I,NPTI_F
!             WRITE(*,400)nn+k,CX(K),CY(K),SOL(K),DSOL(K,1),DSOL(K,2)
!  400        FORMAT(i5,5(3X,E12.5))
!  20      CONTINUE

 50      CONTINUE
!          WRITE(IMP,600)
!  600     FORMAT(' ',80('*'))

      endif





c     Saida do potencial para pos-processamento no gmsh

C	Zera e Acumula potenciais nos nos dos contornos
	do iel=1,nn
		potno(iel)= 0.0
		dpotno(iel,1)= 0.0
		dpotno(iel,2)= 0.0
	end do

	do iel=1,nn

           ino1= inc(iel,1)
           ino2= inc(iel,2)

           potno(ino1)= potno(ino1) + 0.5*fi(iel)
           potno(ino2)= potno(ino2) + 0.5*fi(iel)

C     Calculo das comp. do vetor normal (xnx e xny)
           dx= x(ino2) - x(ino1)
           dy= y(ino2) - y(ino1)
           xl= sqrt(dx*dx + dy*dy)

           xnx=  dy/xl
           xny=  -dx/xl
           
!      Componentes nodais do fluxo : 1/2 da projecao de du/dn em x e em y para cada noh geometrico * (- condutividade)  
! {* fluxo = - k grad u *}
           dpotno(ino1,1)= dpotno(ino1,1) -C0 * 0.5*xnx*dfi(iel)
           dpotno(ino2,1)= dpotno(ino2,1) -C0 * 0.5*xnx*dfi(iel)
               
           dpotno(ino1,2)= dpotno(ino1,2) -C0 * 0.5*xny*dfi(iel)
           dpotno(ino2,2)= dpotno(ino2,2) -C0 * 0.5*xny*dfi(iel)

	end do

	write(nu_trip,*)' View " '//file_bas//' Potencial "{ '
	write(nu_trif,*)' View " '//file_bas//' Fluxo "{ '


	do i=1,neld
		do inol=1,3
                   inog= incd(i,inol)
			
			if(inog.le.nn) then
				
				coord(inol,1)=  x(inog)
				coord(inol,2)=  y(inog)
                               
                                pot(inol)  = potno(inog)
				dpot(inol,1) = dpotno(inog,1)
				dpot(inol,2) = dpotno(inog,2)
! 				write(*,*) i,x(inog),y(inog),potno(inog)
			
			else 
				coord(inol,1)=  cx(inog-nn)
				coord(inol,2)=  cy(inog-nn)
                                pot(inol)  =  sol(inog-nn)
 				dpot(inol,1) = dsol(inog-nn,1)
 				dpot(inol,2) = dsol(inog-nn,2)
! 				write(*,*) i,coord(inol,1),coord(inol,2),sol(inog-nn)
			end if
		enddo

		write(nu_trip,660)coord(1,1),coord(1,2),0.0,
     &                  coord(2,1),coord(2,2),0.0,
     &                  coord(3,1),coord(3,2),0.0,
     &                    pot(1),pot(2),pot(3)
 660		format('ST(',8(F10.4,','),F10.4,'){',2(F10.4,','),F10.4,'};')

		write(nu_trif,670) coord(1,1),coord(1,2),0.0,
     &                             coord(2,1),coord(2,2),0.0,
     &                             coord(3,1),coord(3,2),0.0,
     &                              dpot(1,1),dpot(1,2),0.0,
     &                              dpot(2,1),dpot(2,2),0.0,
     &                              dpot(3,1),dpot(3,2),0.0

 670		format('VT(',8(F10.4,','),F10.4,'){',8(F10.4,','),F10.4,'};')
	enddo

	write(nu_trip,680)
	write(nu_trif,680)
 680   format('};')



	close(nu_msc)
	close(nu_msd)
	close(nu_trip)
	close(nu_trif)





      RETURN
      END
